/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rtsgame.core.view;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import rtsgame.core.view.graphics.GraphicElement;
import rtsgame.core.view.graphics.MovePointer;
import rtsgame.core.view.graphics.ShapeElement;

/**
 *
 * @author tom
 */
public class GameView implements Runnable, ImageObserver, KeyListener, PanelRepaintInteface, MouseListener {

  final int SCREEN_WIDTH = 500;
  final int SCREEN_HEIGHT = 500;
  private GamePanel mainPanel;
  private Image background = new BufferedImage(SCREEN_WIDTH, SCREEN_HEIGHT, BufferedImage.TYPE_INT_ARGB);
  private Image backbuffor = new BufferedImage(SCREEN_WIDTH, SCREEN_HEIGHT, BufferedImage.TYPE_INT_ARGB);
  private ArrayList<Sprite> sprites = new ArrayList<Sprite>();
  private Sprite sprite1;
  private Sprite sprite2;
  private Sprite pointer;

  public GameView(GamePanel mainPanel) {
    this.mainPanel = mainPanel;
    mainPanel.setFocusable(true);
    mainPanel.addKeyListener(this);
    mainPanel.addMouseListener(this);

    GraphicElement element = new ShapeElement(new Rectangle(20, 20), Color.red);
    sprite1 = new Sprite(element);
    sprite1.setX(100);
    sprite1.setY(100);
    sprite1.setState(SpriteState.eSelect);

    element = new ShapeElement(new Rectangle(20, 20), Color.yellow);
    sprite2 = new Sprite(element);
    sprite2.setX(200);
    sprite2.setY(100);

    sprites.add(sprite1);
    sprites.add(sprite2);

    pointer = new Sprite( new MovePointer() );
    pointer.setX( 0 );
    pointer.setY( 0 );

    mainPanel.addPanelRepaint(this);


    background.getGraphics().clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

  }

  public void Draw() {


    Graphics2D bkGraphics = (Graphics2D) backbuffor.getGraphics();
    bkGraphics.drawImage(background, 0, 0, this);


    pointer.Draw(bkGraphics);

    for (Sprite sprite : sprites) {
      sprite.Draw(bkGraphics);
    }


    mainPanel.getGraphics().drawImage(backbuffor, 0, 0, this);
  }

  public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    System.out.println("GameView: imageUpdateL Not supported yet.");
    return true;
  }

  Sprite getSelectedSprite() {
    for (Sprite sprite : sprites) {
      if (sprite.getState() == SpriteState.eSelect) {
        return sprite;
      }
    }

    return null;
  }

  boolean isCollision(Sprite sprite) {
    for (Sprite itemSprite : sprites) {
      if (sprite.isCollision(itemSprite)) {
        return true;
      }
    }

    return false;
  }

  public void keyTyped(KeyEvent e) {
  }

  public void keyPressed(KeyEvent e) {

    Sprite selectedSprite = getSelectedSprite();

    if (selectedSprite == null) {
      return;
    }
    selectedSprite.snapPosition();


    switch (e.getKeyCode()) {
      case KeyEvent.VK_LEFT:
        selectedSprite.moveLeft(1, 0);
        break;

      case KeyEvent.VK_RIGHT:
        selectedSprite.moveRight(1, SCREEN_WIDTH);
        break;

      case KeyEvent.VK_DOWN:
        selectedSprite.moveDown(1, SCREEN_HEIGHT);
        break;

      case KeyEvent.VK_UP:
        selectedSprite.moveUp(1, 0);
        break;

    }

    if (isCollision(selectedSprite)) {
      selectedSprite.revPosition();
    }


  }

  public void keyReleased(KeyEvent e) {
  }

  public void mouseClicked(MouseEvent e) {

  }

  public void mouseEntered(MouseEvent e) {

  }

  public void mouseExited(MouseEvent e) {

  }

  public void mousePressed(MouseEvent e) {
    //sprawdzenie czy jakis sprite zosta; zaznaczony
    Sprite selectedSprite = getSelectedSprite();
    if( selectedSprite != null ) selectedSprite.setState(SpriteState.eNoSelect);
    boolean foundSprite = false;
    for( Sprite sprite : sprites)
    {
      if( sprite.hit(e.getX(), e.getY()) )
      {
        sprite.setState(SpriteState.eSelect);
        foundSprite = true;
        break;
      }
    }
    
    if( !foundSprite) {
        if( selectedSprite != null )
        {
            pointer.setState(SpriteState.eSelect);
            pointer.setX( e.getX() );
            pointer.setY( e.getY() );
            selectedSprite.setState(SpriteState.eNoSelect);
        } 
 }

  }

  public void mouseReleased(MouseEvent e) {

  }



  public void repaint(Graphics2D graphics) {
  }



  public void run() {
    while (true) {
      try {
        Thread.currentThread().sleep(50);
        Draw();
      } catch (InterruptedException ex) {
        Logger.getLogger(GameView.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  }
}
